import md5 from 'blueimp-md5'
import constants from '@/constants'
import { sha256 } from 'js-sha256'

const { apiKey } = constants

export default (params = {}) => {
  const keys = Object.keys(params).sort()
  const sign = keys
    .reduce((collect, key) => {
      let item = params[key]
      if (['number', 'string'].includes(typeof item)) {
        item = (item + '').trim()
      }
      collect.push(item)
      return collect
    }, [])
    .join('')
  return sha256(`${md5(encodeURIComponent(sign))}${apiKey}`)
}
